package com.controller;

import com.pojo.Taocan;
import com.pojo.User;
import com.service.TaocanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class TaocanController {
    @Autowired
    private TaocanService taocanService;

    @RequestMapping(value = "/yuliangquery", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> yuliangQuery(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        String usernumber = (String) session.getAttribute("number");


        // 从会话中获取完整的用户对象
        User user = (User) session.getAttribute("user");
        if (user == null) {
            result.put("error", "用户未登录，请先登录！");
            return result;
        }

        // 使用用户对象的卡号进行查询
        Taocan taocan = taocanService.getLiuliangByNumber(user.getNumber());
        // 添加日志，确认 Taocan 对象内容
        System.out.println("查询结果: " + taocan);
        System.out.println("用户号码: " + taocan.getUsernumber());
        if (taocan != null) {
            result.put("taocan", taocan);
        } else {
            result.put("error", "未找到该号码对应的套餐信息");
        }
        return result;
    }
}